package com.example.cnotebook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.cnotebook.pojo.UserInfo;
import com.example.cnotebook.util.OKHttpUtil;
import com.example.cnotebook.util.UserConstant;

import org.json.JSONException;
import org.json.JSONObject;

public class UpdatePasswordActivity extends BaseActivity implements View.OnClickListener, OKHttpUtil.CallBack {
    private ImageView ivBack;
    private EditText etOldPwd;
    private EditText etNewPwd;
    private EditText etConfirmPwd;
    private Button btnConfirm;

    private String newPwd;

    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password);


        ivBack = findViewById(R.id.iv_back);
        etOldPwd = findViewById(R.id.et_old_pwd);
        etNewPwd = findViewById(R.id.et_new_pwd);
        etConfirmPwd = findViewById(R.id.et_confirm_pwd);
        btnConfirm = findViewById(R.id.btn_confirm);

        ivBack.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);

        sp = getSharedPreferences("UserInfo", MODE_PRIVATE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_confirm:
                try {
                    updatePwd();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void updatePwd() throws JSONException {
        UserInfo userInfo = UserConstant.userInfo;
        String oldPwd = etOldPwd.getText().toString();
        newPwd = etNewPwd.getText().toString();
        String confirmPwd = etConfirmPwd.getText().toString();
        if (!oldPwd.equals(userInfo.getPassword())) {
            Toast.makeText(this, "旧密码错误", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!newPwd.equals(confirmPwd)) {
            Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", UserConstant.userInfo.getId());
        jsonObject.put("password", newPwd);
        OKHttpUtil.post("/user/updatePwd", jsonObject, this, this);
    }

    @Override
    public void onSuccess(JSONObject response) throws JSONException {
        Toast.makeText(this, response.getString("data"), Toast.LENGTH_SHORT).show();
        sp.edit().putBoolean("login", false).apply();
        startActivity(new Intent(this, LoginActivity.class));
        finish();
    }

    @Override
    public void onFailure(int errorNo, String strMsg) {

    }
}
